package benchmark

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"os"
	"runtime/debug"
)

func GenerateBenchmarkDatabaseData(refTestId int64) {
	dsn := "root:lyw123456@tcp(127.0.0.1:3306)/openharmony_schema?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		debug.PrintStack()
		fmt.Println(err)
	}
	bionicData := ParseToBenchmarkBean("D:/auto_test/benchmark/result/bionic_benchmark.log", refTestId)
	muslData := ParseToBenchmarkBean("D:/auto_test/benchmark/result/musl_benchmark.log", refTestId)
	if bionicData == nil {
		fmt.Println("bionicData is nil")
		os.Exit(-1)
	}
	if muslData == nil {
		fmt.Println("muslData is nil")
		os.Exit(-1)
	}
	db.Transaction(func(tx *gorm.DB) error {
		if err2 := tx.Table("bionic_benchmark_info").Create(bionicData).Error; err != nil {
			return err2
		}
		if err2 := tx.Table("musl_benchmark_info").Create(muslData).Error; err != nil {
			return err2
		}
		return nil
	})
}
